package com.example.service.impl;

import com.example.domain.ResultData;
import com.example.domain.User;
import com.example.mapper.MessageMapper;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import com.example.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author zhouxl
 * @DATE 2021/1/31
 * @descriptoin
 */
@Component
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Object login(String name, String password) {
        /**
         * 1.如果当前用户不存在，那么注册，并进行登录处理
         * 2.如果当前用户存在，验证密码是否正确
         * 3.密码正确，进行登录处理
         */
        List<User> userList = userMapper.findByName(name);
        if (Objects.isNull(userList) || userList.size() == 0) {
            return register(name, password);
        }
        User user = userList.get(0);
//        if( WebSocketServer.isLogin(user.getUid())){
//            return ResultData.error("当前用户已经登录,请选择另外的用户登录");
//        }
        if (user.getPassword().equals(password)) {
            return ResultData.success(user);
        }
        return ResultData.error("密码不对");
    }

    @Override
    public Object queryFriendsInfo(String uid) {
        ResultData data = new ResultData();
        /*
            1.查询好友信息
            2.查询消息信息
            3.设置消息数量信息
            4.是否在线
         */
        Map<String, Object> leftUserArray = new HashMap<>();
        // 查询好友信息
        List<Map<String, Object>> friends = userMapper.queryFriendsInfo(uid);
        for (Map<String, Object> map : friends) {
            Map<String, Object> msgVo = new HashMap<>();
            String friendId = map.get("uid").toString();
            map.put("onlion", WebSocketServer.isLogin(map.get("uid").toString()) ? 1 : 0);

            // 构建查询参数
            Map<String, Object> params = new HashMap<>();
            params.put("fromUser", uid);
            params.put("toUser", map.get("uid"));

            msgVo.put("friend", map);
            // 查询消息信息
//            List<Map<String,Object>> noReadTextMessages = messageMapper.queryNoReadTextMessage(params);
            List<Map<String, Object>> noReadTextMessages = new ArrayList<>();
            /**
             * TODO 做点测试数据
             */
            msgVo.put("messages", noReadTextMessages);
            // 设置消息数量信息
            msgVo.put("msg_num", Objects.isNull(noReadTextMessages) ? 0 : noReadTextMessages.size());
            // 是否在线
            msgVo.put("onlion", WebSocketServer.isLogin(friendId) ? 1 : 0);
            leftUserArray.put(friendId, msgVo);
        }
        // TODO 处理在线和未在线
        data.setData(leftUserArray);
        return data;
    }

    @Override
    public Object searchFriends(String name, String uid) {
        ResultData success = ResultData.success(null);
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        params.put("uid", uid);
        List<Map<String, Object>> friends = userMapper.searchFriends(params);
        success.setData(friends);
        return success;
    }


    public Object register(String name, String password) {
        User user = new User();
        user.setUid(UUID.randomUUID().toString().replace("-", ""));
        user.setName(name);
        user.setPassword(password);
        user.setAlias(name);
        user.setEmail("");
        user.setAddress("");
        int count = userMapper.register(user);
        ResultData data = new ResultData();
        data.setMessage(count == 0 ? "登录失败" : "登录成功");
        data.setCode(count == 0 ? 400 : 200);
        data.setData(user);
        return data;
    }

}
